﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.Protocols;
using System.DirectoryServices.AccountManagement;
using ECube.SEC.Models;
namespace ECube.SEC.Services
{
    public class ADAuthenticationUserService : IADAuthenticationUserService
    {
        static Dictionary<string, ADAuthenticationUserService> insServices = new Dictionary<string,ADAuthenticationUserService>(10);
        private PrincipalContext principalContext;
        private ADAuthenticationUserService(ADConnect connect)
        {
            principalContext = new PrincipalContext(connect.ContextType, connect.Name, connect.Container, connect.ContextOptions, connect.Username, connect.Password);
        }
        public static ADAuthenticationUserService GetInstance(ADConnect connect)
        {
            if (connect == null)
            {
                return null;
            }
            if (!insServices.ContainsKey(connect.Key))
            {
                insServices.Add(connect.Key, new ADAuthenticationUserService(connect));
            }
            return insServices[connect.Key];
        }
        #region ILDAPAuthenticationUserService 成员

        public UserPrincipal Valite(string username, string password)
        {
            if (principalContext != null && !string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                try
                {
                    UserPrincipal u = this.GetUser(username);
                    if (u != null)
                    { 
                        if (principalContext.ValidateCredentials(username, password))
                        {
                            return u;
                        }
                    }

                }
                catch (LdapException ex)
                {

                    return null;
                }
            }
            return null;
        }

        public bool ChangePassword(string username,string oldPassword,string newPassword)
        {
            if (principalContext != null)
            {
                UserPrincipal user = new UserPrincipal(principalContext,username, oldPassword,false);
                try
                {
                    user.ChangePassword(oldPassword, newPassword);
                }
                catch (PasswordException ex)
                {
                    //
                }
                return true;
            }
            return false;
        }
        public UserPrincipal GetUser(string username)
        {
            if (principalContext != null)
            {
                UserPrincipal user = UserPrincipal.FindByIdentity(principalContext,IdentityType.SamAccountName,username);
                return user;
            }

            return null;
        }
        
        public GroupPrincipal GetGroup(string groupname)
        {
            throw new NotImplementedException();
        }

       
        public IEnumerable<UserPrincipal> FindUsers(ADUser userFilter)
        {
            if (principalContext != null)
            {
                UserPrincipal user = new UserPrincipal(principalContext);
                PrincipalSearcher ps = user == null ? new PrincipalSearcher() : new PrincipalSearcher(user);
                PrincipalSearchResult<Principal> pu = ps.FindAll();
                return pu.Where(p=>p is UserPrincipal).Select(p=>p as UserPrincipal);                
            }
            return null;
        }

      
        public IEnumerable<GroupPrincipal> FindGroups(ADGroup groupFilter)
        {
            throw new NotImplementedException();
        }

        #endregion
        
    }
}
